package club.easin.product.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import club.easin.product.bean.Soft;
import club.easin.product.service.SoftService;
import club.easin.utils.ReturnData;
import net.bytebuddy.asm.Advice.This;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("soft")
public class SoftController {
	
	@Autowired
	private SoftService softService;
	@RequestMapping(value="getSoft",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getSoft(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String name=object.getString("ranges");
		String stage=object.getString("stage");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("soft");
			List<Map<String,Object>> softList=new ArrayList<Map<String,Object>>();
			List<Soft> list=this.softService.getSoftByName(name, stage);
			for (Soft soft2 : list) {
				Map<String,Object> map=new HashMap<String, Object>();
				map.put("name",soft2.getName());
				map.put("function", soft2.getFunction());
				softList.add(map);
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("softList", softList);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("soft");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	/**根据系列名称和阶段获取使用的软件
	 * @throws Exception */
	@RequestMapping(value="getSofts",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getUseSoft(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String name=object.getString("ranges");
		String stage=object.getString("stage");
		ReturnData data=new ReturnData();
		List<String> anList=new ArrayList<String>();
		try {
			data.setCode(200);
			data.setDesc("soft");
			List<Map<String,Object>> list=this.softService.getUseSoftMap(name, stage);
			Set<String> set=new HashSet<String>();
			for (Map<String,Object> map:list) {
				set.add((String)map.get("description"));
			}
			
			for (String des : set) {
				String str=new String();
				for (Map<String,Object> map : list) {
					if(map.get("description").equals(des)){
						if(map.get("shortname")==null||"".equals(map.get("shortname"))){
							str=str+(String)map.get("name")+"、";
						}else{
							str=str+(String)map.get("shortname")+"、";
						}
					}
				}
				anList.add(des+"使用:"+str);
			}
			
			
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("softList", anList);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("soft");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
	}
	
	
	//根据软件名称获取软件描述和下载地址
	@RequestMapping(value="getSoftDesc",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getSoftDesc(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String name=object.getString("soft");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("softDesc");
			Map<String,Object> map=new HashMap<String, Object>();
			List<Map<String,Object>> list=this.softService.getSoftDescAndDownload(name);
			map.put("descList",list);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("soft");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	//根据软件名称获取软件手册
	@RequestMapping(value="getSoftManual",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getSoftManual(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String name=object.getString("soft");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("softDesc");
			Map<String,Object> map=new HashMap<String, Object>();
			List<Map<String,Object>> list=this.softService.getSoftmanual(name);
			map.put("manualList",list);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("soft");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}

}
